package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {
    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ContractDao contractDao;

   /*
    保存附件
    */
    public void save(ExtCproduct extCproduct) {
        //赋值附件ID
        extCproduct.setId(UUID.randomUUID().toString());
        //计算附件的总金额
        Double money=0.0;
        //判断数量和金额不为0
        if  (extCproduct.getCnumber()!=null&&extCproduct.getPrice()!=null){
            money=extCproduct.getCnumber()*extCproduct.getPrice();
        }
        //赋值附件总金额
        extCproduct.setAmount(money);

        //根据合同ID查询合同信息
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //赋值新的合同总金额
        contract.setTotalAmount(contract.getTotalAmount()+money);
        //更新附件数量
        contract.setExtNum(contract.getExtNum()+1);
        //修改合同数据
        contractDao.updateByPrimaryKeySelective(contract);
        //保存附件数据
        extCproductDao.insertSelective(extCproduct);
    }

    /*
        修改附件
     */
    public void update(ExtCproduct extCproduct) {
        //根据ID查询附件信息
        ExtCproduct oldExtCproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        //获取旧附件的总金额
        Double oldAmount = oldExtCproduct.getAmount();
        //计算修改后的总金额
        Double money=0.0;
        //判断是否为空
        if (extCproduct.getCnumber()!=null&&extCproduct.getPrice()!=null){
            money=extCproduct.getCnumber()*extCproduct.getPrice();
        }
        //赋值新的总金额
        extCproduct.setAmount(money);
        //根据合同ID查询合同信息
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //计算新的合同总金额 合同总金额-旧的附件总金额+新的附件总金额
        contract.setTotalAmount(contract.getTotalAmount()-oldAmount+money);
        //修改附件数据
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
        //修改合同数据
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /*
        删除附件
     */
    public void delete(String id) {
        //根据id查询附件信息
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        //查询合同信息
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //更新合同金额
        contract.setTotalAmount(contract.getTotalAmount()-extCproduct.getAmount());
        //根据合同数据中的附件数量
        contract.setExtNum(contract.getExtNum()-1);
        //删除附件
        extCproductDao.deleteByPrimaryKey(id);
        //更新合同数据
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /*
        根据ID查询附件信息
     */
    public ExtCproduct findById(String id) {
        //根据附件ID查询附件信息
        return extCproductDao.selectByPrimaryKey(id);
    }

    /*
        分页查询附件信息
     */
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        //保存数据
        PageHelper.startPage(page,size);
        //查询所有
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo(list);
    }
}
